package Coffee.dao;

import Coffee.bean.User;
import Coffee.util.Cbase;

import java.util.List;

public class UserDaoImpl extends Cbase implements UserDao{
    @Override
    public void insertUser(User user) {
        String sql = "insert into tb_user (userid,username,userpwd)values(?,?,?)";
        int update = update(sql, user.getUserid(), user.getUsername(), user.getUserpwd());
        if(update == 1){
            System.out.println("添加成功");
        }

    }

    @Override
    public User queryUser(String username) {
        String sql = "select * from tb_user where username = ?";
        return getOne(User.class, sql, username);

    }

    @Override
    public int delUserbyName(String username) {
        String sql = "delete from tb_user where username = ?";
        return update(sql, username);

    }

    @Override
    public int updateUser(String username,String userpwd,String userid) {
        String sql = "update tb_user set username = ?,userpwd = ? where userid = ?";
        return update(sql, username, userpwd,userid);
    }

    @Override
    public List<User> queryAll() {
        String sql = "select * from tb_user";
        return getAll(User.class,sql);
    }

    @Override
    public User login(String username, String userpwd) {
        String sql = "select * from tb_user where username = ? and userpwd = ?";
        return getOne(User.class,sql, username, userpwd);
    }

    @Override
    public int insertMoney(int money,String username) {
        String sql = "update tb_user set money = money+? where username = ?";
        return update(sql, money,username);
    }

    @Override
    public int queryMoneyByUsername(String username) {
        String sql ="select money from tb_user where username = ?";
        User one = getOne(User.class, sql, username);
        return one.getMoney();
    }

    @Override
    public int updateMoney(int money,String username) {
        String sql = "update tb_user set money = money-? where username = ?";
        return update(sql, money,username);
    }
}
